from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.pick_addr_fields_def import pick_addr_fields,pick_addr_list_fields
from services.online_mall.pick_addr_service import PickAddrService



class PickAddrApi(Resource):
    @login_required
    @marshal_with(pick_addr_list_fields)
    @check_perms(perms=['mall:pick_addr:list'])
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("name",type=str,required=False,location='args')
        parser.add_argument("addr",type=str,required=False,location='args')
        parser.add_argument("mobile",type=str,required=False,location='args')
        parser.add_argument("province_id",type=str,required=False,location='args')
        parser.add_argument("city_id",type=str,required=False,location='args')
        parser.add_argument("district_id",type=str,required=False,location='args')
        parser.add_argument("province",type=str,required=False,location='args')
        parser.add_argument("city",type=str,required=False,location='args')
        parser.add_argument("district",type=str,required=False,location='args')
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("page",type=int,required=False,location='args')
        parser.add_argument("page_size",type=int,required=False,location='args')
        args = parser.parse_args()
        return PickAddrService.get_list(current_user,args)
    
    @login_required
    @marshal_with(pick_addr_fields)
    @check_perms(perms=['mall:pick_addr:create'])
    def post(self):
        parser = reqparse.RequestParser()
        # parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("name",type=str,required=True,location='json')
        parser.add_argument("addr",type=str,required=True,location='json')
        parser.add_argument("mobile",type=str,required=True,location='json')
        parser.add_argument("province_id",type=str,required=False,location='json')
        parser.add_argument("city_id",type=str,required=False,location='json')
        parser.add_argument("district_id",type=str,required=False,location='json')
        parser.add_argument("province",type=str,required=False,location='json')
        parser.add_argument("city",type=str,required=False,location='json')
        parser.add_argument("district",type=str,required=False,location='json')
        parser.add_argument("shop_id",type=str,required=False,location='json')
        parser.add_argument("page",type=int,required=False,location='json')
        parser.add_argument("page_size",type=int,required=False,location='json')
        args = parser.parse_args()
        return PickAddrService.create_addr(current_user,args)
    
    @login_required
    @marshal_with(pick_addr_fields)
    @check_perms(perms=['mall:pick_addr:update'])
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        parser.add_argument("name",type=str,required=True,location='json')
        parser.add_argument("addr",type=str,required=True,location='json')
        parser.add_argument("mobile",type=str,required=False,location='json')
        parser.add_argument("province_id",type=str,required=False,location='json')
        parser.add_argument("city_id",type=str,required=False,location='json')
        parser.add_argument("district_id",type=str,required=False,location='json')
        parser.add_argument("province",type=str,required=False,location='json')
        parser.add_argument("city",type=str,required=False,location='json')
        parser.add_argument("district",type=str,required=False,location='json')
        parser.add_argument("shop_id",type=str,required=False,location='json')
        parser.add_argument("page",type=int,required=False,location='json')
        parser.add_argument("page_size",type=int,required=False,location='json')
        args = parser.parse_args()
        return PickAddrService.update_addr(current_user,args)
    
    @login_required
    @marshal_with(pick_addr_fields)
    @check_perms(perms=['mall:pick_addr:del'])
    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        args = parser.parse_args()
        addr_id = args["id"]
        return PickAddrService.delete_addr(current_user,addr_id)
    
api.add_resource(PickAddrApi, '/pick/addr')